cmake_minimum_required(VERSION 3.20)

project(AlexToolkit LANGUAGES CXX VERSION "0.1.0")

set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED ON)

set(QT_QML_GENERATE_QMLLS_INI ON)

configure_file(${CMAKE_CURRENT_SOURCE_DIR}/resources/version_info.rc.in ${CMAKE_CURRENT_BINARY_DIR}/version_info.rc)

set(CMAKE_AUTOMOC ON)
# set(CMAKE_AUTORCC ON)
# set(CMAKE_AUTOUIC ON)
set(CMAKE_INCLUDE_CURRENT_DIR ON)

if(NOT DEV_ROOT) 
set(DEV_ROOT ${CMAKE_CURRENT_LIST_DIR}/3rdparty)
endif()

message(STATUS "dev root: ${DEV_ROOT}")

set(CMAKE_PREFIX_PATH 
${CMAKE_PREFIX_PATH} 
${Qt6_DIR}
${DEV_ROOT}/libssh-dev
${DEV_ROOT}/log4qt-dev
)

set(NPCAP_ROOT ${DEV_ROOT}/npcap-sdk-1.15)
set(libssh_DIR ${DEV_ROOT}/libssh-dev/lib/cmake/libssh)
set(Log4Qt_DIR ${DEV_ROOT}/log4qt-dev)

find_package(Qt6 REQUIRED COMPONENTS Core Quick Network Concurrent Sql SerialPort)
find_package(libssh REQUIRED)
find_package(Log4Qt COMPONENTS log4qt REQUIRED)

qt_standard_project_setup(REQUIRES 6.8)

qt_add_executable(
    ${PROJECT_NAME}
    src/main.cpp
    version_info.rc

    src/alextookit.cpp
    src/alextookit.h

    # common
    src/common/common.h
    src/common/appcfg.h
    src/common/appcfg.cpp
    src/common/utils.h
    src/common/utils.cpp

    # store
    src/store/dbinterface.h
    src/store/dbinterface.cpp
    src/store/dbsqliteinterface.cpp
    src/store/dbsqliteinterface.h
)

qt6_add_qml_module(${CMAKE_PROJECT_NAME}
    URI "Main"
    VERSION 1.0
    RESOURCE_PREFIX "/qt/qml"
    NO_PLUGIN
    QML_FILES 
    qml/Main.qml
)

set_target_properties(${PROJECT_NAME} PROPERTIES LINK_FLAGS_RELEASE "-mwindows")

qt_add_resources(
    ${PROJECT_NAME} "resources"
    PREFIX "/"
    FILES
    resources/qtquickcontrols2.conf
)

target_include_directories(${PROJECT_NAME} PRIVATE 
${NPCAP_ROOT}/Include
${CMAKE_CURRENT_LIST_DIR}/src
${CMAKE_CURRENT_LIST_DIR}/src/host_detector
)
target_link_directories(${PROJECT_NAME} PRIVATE ${NPCAP_ROOT}/Lib/x64)

target_link_libraries(
    ${PROJECT_NAME}
    PRIVATE
        Qt6::Core
        Qt6::Quick
        Qt6::Network
        Qt6::Concurrent
        Qt6::Sql
        Qt6::SerialPort
        
        wpcap
        Packet
        ssh
        Log4Qt::log4qt
)

include(${CMAKE_CURRENT_SOURCE_DIR}/cmake/qmlmodules)

include(GNUInstallDirs)
install(TARGETS ${PROJECT_NAME}
    BUNDLE DESTINATION .
    LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR}
    RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR}
)

# make IDEs aware of the QML import path
set(QML_IMPORT_PATH ${PROJECT_BINARY_DIR}/qml CACHE PATH
    "Path to the custom QML components defined by the project")

message(STATUS "QML_IMPORT_PATH: ${QML_IMPORT_PATH}")

# set(FRAMELESSHELPER_IMPORT_DIR E:/workspace/cpp_project/3rdparty-dev/framelesshelper-dev/qml)
# list(APPEND QML_IMPORT_PATH ${FRAMELESSHELPER_IMPORT_DIR})
# list(REMOVE_DUPLICATES QML_IMPORT_PATH)
# Force cache refresh:
list(APPEND QML_IMPORT_PATH ${PROJECT_BINARY_DIR} ${PROJECT_BINARY_DIR}/qml/imports/ ${PROJECT_BINARY_DIR}/qml/content/ ${PROJECT_BINARY_DIR}/qml/content/pages/)
list(REMOVE_DUPLICATES QML_IMPORT_PATH)

set(QML_IMPORT_PATH ${QML_IMPORT_PATH} CACHE STRING "Qt Creator extra QML import paths" FORCE)

# install(TARGETS ${PROJECT_NAME} DESTINATION .)
# install(FILES ${OPENSSL_PATH}/bin/libcrypto-3-x64.dll DESTINATION .)
# install(DIRECTORY ${CMAKE_CURRENT_LIST_DIR}/tools  DESTINATION .)
# install(IMPORTED_RUNTIME_ARTIFACTS FramelessHelper::FramelessHelperQuick FramelessHelper::FramelessHelperCore DESTINATION .)
